//
//  JDMineViewController.swift
//  DragonWallet
//
//  Created by 无尽宝 on 2024/12/14.
//

import UIKit
import SwiftyJSON

class JDMineViewController: JDBaseViewController {

    var topBgImageView: UIImageView?
    
    var userPortrait: UIImageView?
    
    var userNameLabel: UILabel?
    // 会员图标
    var memberImageV: UIImageView?
    var phoneImageV: UIImageView?
    var phoneLabel: UILabel?
    
    var versionInfoLabel: UILabel?
    
    var mainTableView: UITableView?
    
    var balanceModel: JDBalanceModel?
    
    var titleArray = [[""],["推广码","我的现金账目"],["收货信息","实名认证","服务条款","关于"]]
    lazy var topFuncView: JDMineView = {
        let view = JDMineView()
        view.makeCorner(8)
        view.allOrderButton.addTarget(self, action: #selector(allOrderDetail), for: .touchUpInside)
        view.waitingPayButton.actionControl?.addTarget(self, action: #selector(orderDetail(_:)), for: .touchUpInside)
        view.pickupButton.actionControl?.addTarget(self, action: #selector(orderDetail(_:)), for: .touchUpInside)
        view.completedButton.actionControl?.addTarget(self, action: #selector(orderDetail(_:)), for: .touchUpInside)
        view.alreadyPayButton.actionControl?.addTarget(self, action: #selector(orderDetail(_:)), for: .touchUpInside)
        view.refuseButton.actionControl?.addTarget(self, action: #selector(orderDetail(_:)), for: .touchUpInside)
        
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupUIElements()
        self.setupConfig()
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.phoneLabel?.text = JDLoginModel.default.mobile?.hidePhoneNumber()//"13894887766"
        self.userNameLabel?.text = JDLoginModel.default.nickname//"巴布豆早教中心"
        self.getNetData()
        self.fetchUserBalance()
        printLog("会员等级:\(JDLoginModel.default.memberLevel)")
        if (JDLoginModel.default.memberLevel ?? 0) <= 1 && (JDLoginModel.default.token ?? "").count > 0 {
            self.getUserInfo()
        }
    }
    
    func uploadUserInfo() {
        self.userPortrait?.downloadImage(JDLoginModel.default.headerImg ?? "", "userPortraiPlaceholder")
        if (JDLoginModel.default.memberLevel ?? 0) <= 1 {
            self.memberImageV?.isHidden = true
        } else {
            self.memberImageV?.isHidden = false
        }
    }
    
    func getUserInfo() {
        JDNetWorkRequest(.getUserInfo) { responseStr in
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                if let loginModel = JDLoginModel.deserialize(from: responseStr, designatedPath: "data") {
                    JDLoginModel.default.refreshData(loginModel)
                    self.uploadUserInfo()
                }
            }
        } failure: { error in
            
        } error: { errorCode in
            
        }
    }

    func setupConfig() {
//        self.setupNavigationTitle("我的")
//        self.customNavigationBar?.alpha = 0.0
//        self.customImageRightBarButtonItem("black_back", #selector(settingAction), self)
        
//        self.splitSubviewNavigationbar("black_back", #selector(settingAction), self, "我的")
        
//        self.splitSubviewNavigationbar(nil, nil, nil, "mine_setting", "设置", #selector(settingAction), self, "我的")
        
        self.splitOnlyTitleNavigation("我的")
        self.splitNavigationbarCustomRightImageButton("mine_setting", "设置", #selector(settingAction), self)
        
//        self.view.bringSubviewToFront(self.customNavigationBar!)
        
    }
    
    @objc func settingAction() {
        print("设置按钮")
        
        let userInfoVC = JDUserCenterViewController()
        self.hidesBottomBarPushViewController(userInfoVC)

//        let settingVC = JDSettingViewController()
//        self.hidesBottomBarPushViewController(settingVC)
    }
    
    func setupUIElements() {
        self.topBgImageView = UIImageView()
        self.view.addSubview(self.topBgImageView!)
        self.topBgImageView?.snp.makeConstraints({ make in
            make.leading.trailing.top.equalTo(self.view)
            make.height.equalTo(228)
        })
        self.topBgImageView?.image = UIImage(named: "mine_topBgImage")
        
        self.versionInfoLabel = UILabel()
        self.view.addSubview(self.versionInfoLabel!)
        self.versionInfoLabel?.snp.makeConstraints({ make in
            make.leading.trailing.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-22)
        })
        self.versionInfoLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 11, weight: .regular), textColor: color("#9DA1B2"), numberOfLines: 0)
        self.versionInfoLabel?.text = "大连龙平台网络科技开发有限公司 \n版本 v1.0.0"
        
//        var userNameLabel: UILabel?
//        var phoneImageV: UIImageView?
//        var phoneLabel: UILabel?
        
        self.mainTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.mainTableView!)
        self.mainTableView?.snp.makeConstraints({ make in
            make.leading.equalTo(self.view).offset(12)
            make.trailing.equalTo(self.view).offset(-12)
            make.top.equalTo(self.topBgImageView!.snp.bottom).offset(-44)
            make.bottom.equalTo(self.view).offset(-64)
        })
        self.mainTableView?.delegate = self
        self.mainTableView?.dataSource = self
        self.mainTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
        self.mainTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "topCell")
        self.mainTableView?.separatorInset = UIEdgeInsets(top: 0.01, left: 0.01, bottom: 0.01, right: 0.01)
        self.mainTableView?.backgroundColor = .commonColor()
        self.mainTableView?.showsVerticalScrollIndicator = false
//        self.mainTableView?.isScrollEnabled = false
        self.mainTableView?.separatorStyle = .none
        
        self.mainTableView?.contentInsetAdjustmentBehavior = .never
        if #available(iOS 15.0, *) {
            self.mainTableView?.sectionHeaderTopPadding = .zero
        } else {
            // Fallback on earlier versions
        }
        self.mainTableView?.cellLayoutMarginsFollowReadableWidth = false
        self.mainTableView?.backgroundColor = .clear
        
        
        self.userPortrait = UIImageView()
        self.view.addSubview(self.userPortrait!)
        self.userPortrait?.snp.makeConstraints { make in
            make.leading.equalTo(self.view).offset(25)
            make.size.equalTo(CGSizeMake(64, 64))
            make.bottom.equalTo(self.mainTableView!.snp.top).offset(-19)
        }
        self.userPortrait?.layer.cornerRadius = 32
        self.userPortrait?.clipsToBounds = true
        self.userPortrait?.image = UIImage(named: "userPortraiPlaceholder")
        
        print("头像key:\(JDLoginModel.default.headerImg)")
        
        self.userPortrait?.downloadImage(JDLoginModel.default.headerImg ?? "", "userPortraiPlaceholder")
        
        
        self.phoneImageV = UIImageView()
        self.view.addSubview(self.phoneImageV!)
        self.phoneImageV?.snp.makeConstraints({ make in
//            make.leading.equalTo(self.view).offset(24)
            make.leading.equalTo(self.userPortrait!.snp.trailing).offset(10)
            make.bottom.equalTo(self.mainTableView!.snp.top).offset(-28)
            make.size.equalTo(CGSize(width: 10, height: 12))
        })
        self.phoneImageV?.image = UIImage(named: "mine_phone")?.withRenderingMode(.alwaysOriginal)
        
        self.phoneLabel = UILabel()
        self.view.addSubview(self.phoneLabel!)
        self.phoneLabel?.snp.makeConstraints({ make in
            make.leading.equalTo(self.phoneImageV!.snp.trailing).offset(6)
            make.centerY.equalTo(self.phoneImageV!)
        })
        self.phoneLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: .white, numberOfLines: 1)
        self.phoneLabel?.text = JDLoginModel.default.mobile//"13894887766"
        
        self.userNameLabel =  UILabel()
        self.view.addSubview(self.userNameLabel!)
        self.userNameLabel?.snp.makeConstraints({ make in
            make.leading.equalTo(self.phoneImageV!)
            make.bottom.equalTo(self.phoneImageV!.snp.top).offset(-8)
            make.height.equalTo(28)
        })
        self.userNameLabel?.text = JDLoginModel.default.nickname//"巴布豆早教中心"
        self.userNameLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 20, weight: .semibold), textColor: .white, numberOfLines: 1)
        
        self.memberImageV = UIImageView()
        self.view.addSubview(self.memberImageV!)
        self.memberImageV?.snp.makeConstraints({ make in
            make.leading.equalTo(self.userNameLabel!.snp.trailing).offset(6)
            make.centerY.equalTo(self.userNameLabel!)
            make.size.equalTo(CGSizeMake(50, 18))
        })
        self.memberImageV?.image = UIImage(named: "mine_member")?.withRenderingMode(.alwaysOriginal)
        
        if (JDLoginModel.default.memberLevel ?? 0) <= 1 {
            self.memberImageV?.isHidden = true
        } else {
            self.memberImageV?.isHidden = false
        }
    }
    
    override func getNetData(_ isRefresh: Bool = true) {
        JDNetWorkRequest(.fetchOrderCount) { responseStr in
            print("返回参数:\(responseStr)")
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                // 待付款订单数量
                let pendingOrderCount = JSON(commonModel?.data as Any).dictionaryValue["pendingOrderCount"]?.intValue
                self.topFuncView.waitingPayButton.number = pendingOrderCount ?? 0
//                commonModel?.data
                // 已付款订单数量
                let paidOrderCount = JSON(commonModel?.data as Any).dictionaryValue["paidOrderCount"]?.intValue
                self.topFuncView.alreadyPayButton.number = paidOrderCount ?? 0
            }
            
        } failure: { error in
            self.view.toast(message: error.localizedDescription)
        } error: { errorCode in
            self.view.toast(message: "网络异常:\(errorCode)")
        }

    }
    
    /// 获取用户账户余额，可提现金额
    func fetchUserBalance() {
        JDNetWorkRequest(.fetchUserBalance([:])) { responseStr in
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                if let balanceModel = JDBalanceModel.deserialize(from: responseStr, designatedPath: "data") {
                    
                    self.balanceModel = balanceModel
                    self.mainTableView?.reloadRows(at: [IndexPath(row: 1, section: 1)], with: .none)
                }
            }
            
        } failure: { error in
            
        } error: { errorCode in
            
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    @objc func allOrderDetail() {
        let orderList = JDOrderListViewController()
        orderList.currentIndex = 0
        self.hidesBottomBarPushViewController(orderList)
    }
    
    @objc func orderDetail(_ control: UIControl) {
        
        print("点击:\(control.tag)")
        let orderList = JDOrderListViewController()
        orderList.currentIndex = control.tag
        
        self.hidesBottomBarPushViewController(orderList)
    }
        

}

extension JDMineViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.titleArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if self.titleArray.count > section {
            return self.titleArray[section].count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "topCell")
            
            for subV in cell?.contentView.subviews ?? [UIView]() {
                subV.removeFromSuperview()
            }
            
            // TODO: 这里都加个底部容器吧
            let containerView = UIView()
            cell?.contentView.addSubview(containerView)
            containerView.snp.makeConstraints { make in
                make.leading.trailing.top.bottom.equalToSuperview()
            }
            containerView.backgroundColor = .white
            containerView.makeCorner(8)
            
            cell?.contentView.addSubview(self.topFuncView)
            self.topFuncView.snp.makeConstraints { make in
                make.leading.trailing.equalTo(cell!.contentView)
                make.top.bottom.equalTo(cell!.contentView)
            }
            self.topFuncView.backgroundColor = .clear
            cell?.contentView.backgroundColor = .clear
            cell?.backgroundColor = .clear
//            cell?.contentView.clipsToBounds = true
//            self.topFuncView.makeCorner(8)

            cell?.selectionStyle = .none
            
//            if indexPath.row == 0 {
//                cell?.contentView.makeBezierPathCorner([.topLeft,.topRight], 8)
//            } else if indexPath.row+1 == self.titleArray[indexPath.section].count {
//                cell?.contentView.makeBezierPathCorner([.bottomLeft,.bottomRight], 8)
//            }
            
            
            return cell!
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "myCell")
            cell?.selectionStyle = .none
                        
            for subV in cell?.contentView.subviews ?? [UIView]() {
                subV.removeFromSuperview()
            }
            cell?.contentView.backgroundColor = .clear
            cell?.backgroundColor = .clear
            
            let titleStr = self.titleArray[indexPath.section][indexPath.row]
            
            let containerView = UIView()
            cell?.contentView.addSubview(containerView)
            containerView.snp.makeConstraints { make in
                make.leading.top.equalToSuperview()
                make.width.equalTo(screenWidth-24)
                make.height.equalTo(55)
            }
            containerView.backgroundColor = .white
//            containerView.makeCorner(8)
            
            if indexPath.row == 0 {
                containerView.makeBezierPathCorner([.topLeft,.topRight], 8)
            } else if indexPath.row+1 == self.titleArray[indexPath.section].count {
                containerView.makeBezierPathCorner([.bottomLeft,.bottomRight], 8)
            }
            let leftLabel = UILabel()
            cell?.contentView.addSubview(leftLabel)
            leftLabel.snp.makeConstraints { make in
                make.leading.equalTo(cell!.contentView).offset(12)
                make.centerY.equalTo(cell!.contentView)
            }
            leftLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 15, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
            leftLabel.text = titleStr
            
            let rightArrow = UIImageView()
            cell?.contentView.addSubview(rightArrow)
            rightArrow.snp.makeConstraints { make in
                make.trailing.equalTo(cell!.contentView).offset(-12)
                make.centerY.equalTo(cell!.contentView)
                make.size.equalTo(CGSizeMake(5, 9))
            }
            rightArrow.image = UIImage(named: "arrow_right")?.withRenderingMode(.alwaysOriginal)
            
            
            if indexPath.row != (self.titleArray[indexPath.section].count-1) {
                let bottomLine = UIView()
                cell?.contentView.addSubview(bottomLine)
                bottomLine.snp.makeConstraints { make in
                    make.leading.equalTo(cell!.contentView).offset(12)
                    make.trailing.equalTo(cell!.contentView).offset(-12)
                    make.bottom.equalTo(cell!.contentView)
                    make.height.equalTo(1)
                }
                bottomLine.backgroundColor = color("#F5F6F8")
            }
            
            if indexPath.section == 1 && indexPath.row == 1 {
                let moneyLabel = UILabel()
                cell?.contentView.addSubview(moneyLabel)
                moneyLabel.snp.makeConstraints { make in
                    make.trailing.equalTo(cell!.contentView).offset(-25)
                    make.centerY.equalTo(cell!.contentView)
                }
                moneyLabel.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 15, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
                moneyLabel.text = String(format: "￥%@", self.balanceModel?.balanceText ?? "0.00")
                moneyLabel.priceString(UIFont.systemFont(ofSize: 12, weight: .regular), UIFont.systemFont(ofSize: 15, weight: .regular))
            }
            
    
            return cell!
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 161
        } else if indexPath.section == 1 {
            return 55
        } else if indexPath.section == 2 {
            return 55
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 1:
            switch indexPath.row {
            case 0:
                let QRCodeView = JDDownLoadQRCode()
                JDTools.getKeyWindows()?.rootViewController?.view.addSubview(QRCodeView)
            case 1:
                let commissionVC = JDMyCommissionViewController()
                self.hidesBottomBarPushViewController(commissionVC)
            default:
                break
            }
        case 2:
            switch indexPath.row {
            case 0:
                let addressListVC = JDAddressListViewController()
                self.hidesBottomBarPushViewController(addressListVC)
            case 1:
                printLog("实名认证")
                let authenticationListVC = JDAuthenticationListViewController()
                self.hidesBottomBarPushViewController(authenticationListVC)
            case 2:
                printLog("服务条款")
                let termVC = JDTermViewController()
                self.hidesBottomBarPushViewController(termVC)
            case 3:
                printLog("关于")
                let aboutVC = JDAboutViewController()
                self.hidesBottomBarPushViewController(aboutVC)
            default:
                break
            }
        default:
            break
        }
    }
    
}

class JDMineView: UIView {
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "我的订单"
        label.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 14, weight: .semibold), textColor: color("#4D0F0B"), numberOfLines: 1)
        return label
    }()
    
    lazy var rightArrow: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "arrow_right")
        return imageV
    }()
    
    lazy var allOrderButton: UIButton = {
        let button = UIButton(type: .custom)
        button.buttonConfig(normalTitle: "全部", normalImageNamed: "", normalTitleColor: color("#4D0F0B"), titleFont: UIFont.systemFont(ofSize: 14, weight: .regular))
        return button
    }()
    
    lazy var topLine: UIView = {
        let line = UIView()
        line.backgroundColor = color("#F2EDED")
        return line
    }()
    
    lazy var stackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.alignment = .fill
        stack.spacing = 0.0
        stack.distribution = .fillEqually
        return stack
    }()
    /// 待付款
    lazy var waitingPayButton: JDFuncButton = {
        let button = JDFuncButton()
        button.buttonImageV.image = UIImage(named: "order_waitingPay")
        button.titleLabel.text = "待付款"
        button.actionControl?.tag = 1
        return button
    }()
    /// 已付款
    lazy var alreadyPayButton: JDFuncButton = {
        let button = JDFuncButton()
        button.buttonImageV.image = UIImage(named: "order_alreadyPay")
        button.titleLabel.text = "待发货"
        button.actionControl?.tag = 2
        return button
    }()
    /// 待取货
    lazy var pickupButton: JDFuncButton = {
        let button = JDFuncButton()
        button.buttonImageV.image = UIImage(named: "order_pickup")
        button.titleLabel.text = "待收货"
        button.actionControl?.tag = 3
        return button
    }()
    /// 已完成
    lazy var completedButton: JDFuncButton = {
        let button = JDFuncButton()
        button.buttonImageV.image = UIImage(named: "order_completed")
        button.titleLabel.text = "已完成"
        button.actionControl?.tag = 4
        return button
    }()
    
    /// 已拒单
    lazy var refuseButton: JDFuncButton = {
        let button = JDFuncButton()
        button.buttonImageV.image = UIImage(named: "order_refuse")
        button.titleLabel.text = "已拒单"
        button.actionControl?.tag = 5
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIEelements()
    }
    
    func setupUIEelements() {
        
        self.addSubview(self.topLine)
        self.topLine.snp.makeConstraints { make in
            make.leading.equalTo(self).offset(12)
            make.trailing.equalTo(self).offset(-12)
            make.top.equalTo(self).offset(44)
            make.height.equalTo(0.5)
        }
        
        self.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(self).offset(13)
            make.top.equalTo(self)
            make.bottom.equalTo(self.topLine.snp.top)
        }
        self.titleLabel.text = "我的订单"
        
        
        self.addSubview(self.rightArrow)
        self.rightArrow.snp.makeConstraints { make in
            make.centerY.equalTo(self.titleLabel)
            make.trailing.equalTo(self).offset(-12)
            make.size.equalTo(CGSizeMake(5, 9))
        }
        
        self.addSubview(self.allOrderButton)
        self.allOrderButton.snp.makeConstraints { make in
            make.centerY.equalTo(self.titleLabel)
            make.trailing.equalTo(self.rightArrow.snp.leading).offset(-6)
        }
        
        self.addSubview(self.stackView)
        self.stackView.snp.makeConstraints { make in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self.topLine.snp.bottom)
            make.bottom.equalTo(self)
        }
        
        self.waitingPayButton.frame = CGRect(x: 0, y: 0, width: frame.size.width/5.0, height: 116)
        self.alreadyPayButton.frame = CGRect(x: 0, y: 0, width: frame.size.width/5.0, height: 116)
        self.pickupButton.frame = CGRect(x: 0, y: 0, width: frame.size.width/5.0, height: 116)
        self.completedButton.frame = CGRect(x: 0, y: 0, width: frame.size.width/5.0, height: 116)
        self.refuseButton.frame = CGRect(x: 0, y: 0, width: frame.size.width/5.0, height: 116)
       
        self.stackView.addArrangedSubview(self.waitingPayButton)
        self.stackView.addArrangedSubview(self.alreadyPayButton)
        self.stackView.addArrangedSubview(self.pickupButton)
        self.stackView.addArrangedSubview(self.completedButton)
        self.stackView.addArrangedSubview(self.refuseButton)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// 高度必须是116
class JDFuncButton: UIView {
    
    var number: Int = 0 {
        didSet {
            numberLabel.text = String(format: "%d", number)
            if number > 0 {
                numberLabel.isHidden = false
            } else {
                numberLabel.isHidden = true
            }
        }
    }
    
    lazy var numberLabel: JDPaddingLabel = {
        let label = JDPaddingLabel()
        label.edgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        label.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 13, weight: .semibold), textColor: .white, numberOfLines: 1)
        label.backgroundColor = color("#FF5632")
        label.makeBorder(.white, 1.5)
        label.makeCorner(10)
        label.isHidden = true
        return label
    }()
    
    lazy var buttonImageV: UIImageView = {
        let imageV = UIImageView()
        
        return imageV
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        return label
    }()
    
    var actionControl: UIControl?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIElements()
        self.actionControl = UIControl(frame: CGRect(x: 0, y: 0, width: (screenWidth-24)/4.0, height: 116))
        self.addSubview(self.actionControl!)
    }
    
    func setupUIElements() {
        
        self.addSubview(self.buttonImageV)
        self.buttonImageV.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.size.equalTo(CGSize(width: 40, height: 40))
            make.top.equalTo(self).offset(25)
        }
        
        self.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.top.equalTo(self.buttonImageV.snp.bottom).offset(8)
            make.height.equalTo(20)
        }
        self.titleLabel.text = "待付款"
        
        self.addSubview(self.numberLabel)
        self.numberLabel.snp.makeConstraints { make in
            make.right.equalTo(self.buttonImageV).offset(5)
            make.top.equalTo(self.buttonImageV).offset(-5)
            make.width.greaterThanOrEqualTo(20)
            make.height.equalTo(20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
